/*
 * 文件名：DatabaseServer.java
 * 版权：亚信科技(南京)版权所有
 * 描述：TODO
 * 修改人：huangxd2
 * 修改时间：2014年11月2日
 * 修改内容：TODO
 */
package com.ailk.bigdata.etl.realstream.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ailk.bigdata.etl.realstream.server.model.RealsocksCalc;

/**
 * @description TODO
 * @author [huangxd2]
 * @version [版本号,2014年11月2日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class DatabaseServer
{
	private static final Logger logger = LoggerFactory.getLogger(DatabaseServer.class);
	private Selector selector = null;
	private ServerSocketChannel serverSocketDatatransChannel = null;

	private DatabaseServer(RealsocksCalc Realsockcalc) throws IOException
	{
		selector = Selector.open();
		serverSocketDatatransChannel = ServerSocketChannel.open();
		serverSocketDatatransChannel.socket().setReuseAddress(true);
		serverSocketDatatransChannel.configureBlocking(false);
		serverSocketDatatransChannel.socket().bind(new InetSocketAddress(Realsockcalc.getDataTransPort()));
	}

	public void service()
	{

		try
		{
			serverSocketDatatransChannel.register(selector, SelectionKey.OP_ACCEPT);
			while (selector.select() > 0)
			{
				Set<?> readyKeys = selector.selectedKeys();
				Iterator<?> it = readyKeys.iterator();
				while (it.hasNext())
				{
					SelectionKey key = null;
					key = (SelectionKey) it.next();
					it.remove();

					if (key.isAcceptable())
					{
						ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
						SocketChannel socketChannel = (SocketChannel) ssc.accept();
						System.out.println("接收到客户连接，来自:" + socketChannel.socket().getInetAddress() + ":" + socketChannel.socket().getPort());
						socketChannel.configureBlocking(false);
						ByteBuffer buffer = ByteBuffer.allocate(1024);
						socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, buffer);
					}
					if (key.isReadable())
					{
						receiveData(key);
					}

				}

			}

		}
		catch (ClosedChannelException e)
		{
			// TODO Auto-generated catch block
			logger.error("注册服务事件失败", e);
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void receiveData(SelectionKey key) throws IOException
	{
		ByteBuffer buffer = (ByteBuffer) key.attachment();
		SocketChannel socketChannel = (SocketChannel) key.channel();
		ByteBuffer readBuff = ByteBuffer.allocate(32);
		socketChannel.read(readBuff);
		readBuff.flip();
		buffer.limit(buffer.capacity());
		buffer.put(readBuff);
	}

	public static void main(String args[])
	{
		RealsocksCalc rc=new RealsocksCalc();
		rc.setDataTransPort(8000);
		try
		{
			DatabaseServer ds=new DatabaseServer(rc);
			ds.service();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
